package gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import util.ImageLoader;

import controller.AbstractController;

public class MouseControlPanel {
	
	private final AbstractController placeNewController;
	private final AbstractController placeFreeController;
	private final AbstractController selectionModeController;
	private final AbstractController transformController;
	
	private final InputAdapter adapter;
	
	private AbstractController currentActive = null;
	
	private static final String PLACE_NODES_TOOLTIP = "<html>Left Button to build from a Node<br>Right button to deselect</html>";
	
	private Box box;
	
	public MouseControlPanel(InputAdapter adapter, AbstractController placeNewController, AbstractController placeFreeController, AbstractController selectionModeController, AbstractController transformController) {
		this.placeNewController = placeNewController;
		this.placeFreeController = placeFreeController;
		this.selectionModeController = selectionModeController;
		this.transformController = transformController;
		
		this.adapter = adapter;
		
		this.box = Box.createHorizontalBox();
		ImageLoader loader = new ImageLoader("/resource/image/menu/");
		
		ButtonGroup group = new ButtonGroup();
		Insets noInset = new Insets(0, 0, 0, 0);
		JToggleButton placeNewMode = new ImageToggleButton(loader.load("placeNode.png"));
		placeNewMode.setToolTipText(PLACE_NODES_TOOLTIP);
		placeNewMode.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				setActive(MouseControlPanel.this.placeNewController);
			}
		});
		
		JToggleButton placeFreeMode = new ImageToggleButton(loader.load("freePlace.png"));
		placeFreeMode.setToolTipText("Click to place new nodes.");
		placeFreeMode.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				setActive(MouseControlPanel.this.placeFreeController);
			}
		});
		
		JToggleButton selectionMode = new ImageToggleButton(loader.load("select.png"));
		selectionMode.setToolTipText("Select nodes to delete or construct structures.");
		selectionMode.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				setActive(MouseControlPanel.this.selectionModeController);
			}
		});

		JToggleButton transformMode = new ImageToggleButton(loader.load("transform.png"));
		transformMode.setMargin(noInset);
		transformMode.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				setActive(MouseControlPanel.this.transformController);
			}
		});
		group.add(placeNewMode);
		group.add(placeFreeMode);
		group.add(selectionMode);
		group.add(transformMode);
		
		box.add(placeNewMode);
		box.add(placeFreeMode);
		box.add(selectionMode);
		box.add(transformMode);
		
	}
	
	public Box getPanel() {
		return box;
	}
	
	private void setActive(AbstractController controller) {
		if (controller == currentActive) return;
		clearActive();
		
		currentActive = controller;
		currentActive.activate();
		adapter.addInputListener(controller);
	}
	
	public void clearActive() {
		if (currentActive != null) {
			currentActive.deactivate();
			adapter.removeInputListener(currentActive);
			currentActive = null;
		}
	}

}
